<template>
  <div :class="className" :style="{height:height,width:width}"></div>
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import { debounce } from '@/utils'
import { placeUsageRate } from '../../../../api/dashboard/placeUsageRate'
import '../../../../utils/getTime'

export default {
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '350px'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Object
    }
  },
  data() {
    return {
      chart: null
    }
  },
  mounted() {
    var arrTime = []
    var arrValue = []
    for (var i = 0; i < 5 ; i++) {
      const time = new Date().format('yyyy-MM-dd hh:mm:ss', i)
      placeUsageRate(time).then(response => {
        arrValue.push(response)
      })
      arrTime.push(time)
    }
    arrValue.reverse()
    arrTime.reverse()
    this.initChart(arrTime, arrValue)
    if (this.autoResize) {
      this.__resizeHanlder = debounce(() => {
        if (this.chart) {
          this.chart.resize()
        }
      }, 100)
      window.addEventListener('resize', this.__resizeHanlder)
    }
    // 监听侧边栏的变化
    const sidebarElm = document.getElementsByClassName('sidebar-container')[0]
    sidebarElm.addEventListener('transitionend', this.__resizeHanlder)
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    if (this.autoResize) {
      window.removeEventListener('resize', this.__resizeHanlder)
    }

    const sidebarElm = document.getElementsByClassName('sidebar-container')[0]
    sidebarElm.removeEventListener('transitionend', this.__resizeHanlder)

    this.chart.dispose()
    this.chart = null
  },
  watch: {
  },
  methods: {
    initChart(arrTime, arrValue) {
      console.log(arrTime)
      console.log(arrValue)
      this.chart = echarts.init(this.$el, 'macarons')
      this.chart.setOption({
        xAxis: {
          data: arrTime,
          boundaryGap: false,
          axisTick: {
            show: false
          }
        },
        visualMap: {
          show: false,
          pieces: [{
            gt: 0,
            lte: 0.4,
            color: 'blue'
          }, {
            gt: 0.4,
            color: 'red'
          }],
          seriesIndex: 0
        },
        grid: {
          left: 10,
          right: 10,
          bottom: 20,
          top: 30,
          containLabel: true
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross'
          },
          padding: [5, 10]
        },
        yAxis: {
          axisTick: {
            show: false
          },
          axisLine: {
            lineStyle: {
              width: 3,
              color: {
                type: 'linear',
                x: 0,
                y: 0,
                x2: 0,
                y2: 1,
                colorStops: [{
                  offset: 0,
                  color: 'red'
                }, {
                  offset: 1,
                  color: 'blue'
                }],
                globalCoord: false // 缺省为 false
              }
            }
          }
        },
        series: [{
          name: '周转率',
          smooth: true,
          type: 'line',
          data: arrValue,
          animationDuration: 2800,
          animationEasing: 'cubicInOut',
          itemStyle: {
            normal: {
              color: 'rgb(103, 99, 99)',
              shadowBlur: 2,
              shadowColor: 'rgba(0, 0, 0, .12)',
              shadowOffsetX: 2,
              shadowOffsetY: 2
            }
          },
          lineStyle: {
            normal: {
              width: 3,
              shadowColor: 'rgba(0,0,0,0.4)',
              shadowBlur: 10,
              shadowOffsetX: 4,
              shadowOffsetY: 10
            }
          },
          markLine: {
            silent: true,
            data: [{
              yAxis: 0.4
            }]
          }
        }]
      })
    }
  }
}
</script>
